package com.micro.ai.models.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.models.entity.ModelType;
import com.micro.ai.models.service.ModelTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 模型类型控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/model-types")
@Tag(name = "模型类型管理", description = "模型类型的增删改查")
public class ModelTypeController {

    @Autowired
    private ModelTypeService modelTypeService;

    @PostMapping
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:type:create')")
    @Operation(summary = "创建模型类型", description = "创建新的模型类型，如文本生成、图像识别等")
    public ApiResponse<ModelType> create(@Valid @RequestBody ModelType modelType) {
        log.info("创建模型类型请求: tenantId={}, typeCode={}, typeName={}", 
                modelType.getTenantId(), modelType.getTypeCode(), modelType.getTypeName());
        ModelType result = modelTypeService.create(modelType);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:type:update')")
    @Operation(summary = "更新模型类型", description = "更新模型类型信息")
    public ApiResponse<ModelType> update(
            @Parameter(description = "类型ID", required = true) @PathVariable String id,
            @Valid @RequestBody ModelType modelType) {
        log.info("更新模型类型请求: typeId={}", id);
        
        ModelType result = modelTypeService.update(id, modelType);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasAuthority('model:type:delete')")
    @Operation(summary = "删除模型类型", description = "删除模型类型")
    public ApiResponse<Void> delete(
            @Parameter(description = "类型ID", required = true) @PathVariable String id) {
        log.info("删除模型类型请求: typeId={}", id);
        
        modelTypeService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('model:type:view')")
    @Operation(summary = "获取模型类型详情", description = "根据ID获取模型类型详细信息")
    public ApiResponse<ModelType> getById(
            @Parameter(description = "类型ID", required = true) @PathVariable String id) {
        log.info("查询模型类型详情: typeId={}", id);
        
        ModelType modelType = modelTypeService.getById(id);
        if (modelType == null) {
            return ApiResponse.error("404", "模型类型不存在");
        }
        
        return ApiResponse.success(modelType);
    }

    @GetMapping
    @PreAuthorize("hasRole('ADMIN') or hasRole('USER') or hasAuthority('model:type:view')")
    @Operation(summary = "分页查询模型类型", description = "根据条件分页查询模型类型列表")
    public ApiResponse<Page<ModelType>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(required = false) String tenantId,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        log.info("分页查询模型类型: pageNum={}, pageSize={}, tenantId={}, keyword={}", 
                pageNum, pageSize, tenantId, keyword);
        
        Page<ModelType> page = modelTypeService.pageQuery(pageNum, pageSize, tenantId, keyword);
        return ApiResponse.success(page);
    }

}

